package com.atguigu.gulimall.product.service.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.atguigu.gulimall.product.vo.SkuItemVO.SpuItemAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //key 对应的数据是前端传过来的查询参数
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        // key不为空
        if (!StringUtils.isEmpty(key)) {
            /**
             * 注意，此处直接使用eq的方式，和使用wrapper.and的方式，所构造出来的SQL语句有所不同
             *
             * wrapper.eq("attr_group_id", key).or().like("attr_group_name", key)所构造的语句：
             * SELECT
             * attr_group_id,attr_group_name,sort,descript,icon,catelog_id
             * FROM pms_attr_group
             * WHERE
             * (
             * 	attr_group_id = ? OR attr_group_name LIKE ? AND catelog_id = ?
             * );
             *---------------------------------------------------------------------------------------------------------
             * wrapper.and((obj) -> {obj.eq("attr_group_id", key).or().like("attr_group_name", key);});所构造的语句：
             * SELECT
             * attr_group_id,attr_group_name,sort,descript,icon,catelog_id
             * FROM pms_attr_group
             * WHERE
             * (
             * 	(attr_group_id = ? OR attr_group_name LIKE ?)
             * 	AND catelog_id = ?
             * );
             * */
            //wrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
            wrapper.and((obj) -> {
                //上面使用了and，那就会将下面这个条件判断语句用一个括号包起来
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            // Query可以把map封装为IPage
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        } else {
            // 增加id信息
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVO> collect = groupEntities.stream().map(group -> {
            AttrGroupWithAttrsVO attrsVO = new AttrGroupWithAttrsVO();
            BeanUtils.copyProperties(group, attrsVO);
            List<AttrEntity> attrList = attrService.getRelationAttr(attrsVO.getAttrGroupId());
            attrsVO.setAttrs(attrList);
            return attrsVO;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
     * */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroupVo> vos = this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return vos;
    }

}
